package leetcode.code1277;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

//执行用时：5 ms, 在所有 Java 提交中击败了99.70%的用户
//
//内存消耗：49.2 MB, 在所有 Java 提交中击败了80.33%的用户
//
//通过测试用例：32 / 32
public class Solution implements IDebug {

	public int countSquares(int[][] matrix) {
		int ans = 0;
		int m = matrix.length, n = matrix[0].length;
		for (int i = 0; i < m; i++) {// 这两不能都从0开始
			if (matrix[i][0] == 1) {
				ans++;
			}
		}
		for (int j = 1; j < n; j++) {// 这两不能都从0开始
			if (matrix[0][j] == 1) {
				ans++;
			}
		}
		for (int i = 1; i < m; i++) {
			for (int j = 1; j < n; j++) {
				if (matrix[i][j] == 1) {
					int min = Math.min(matrix[i - 1][j], matrix[i][j - 1]);
					min = Math.min(matrix[i - 1][j - 1], min) + 1;
					matrix[i][j] = min;
					ans += min;
				}
			}
		}
//		H.printObject(matrix);
		return ans;
	}

	@Override
	public void debug1() {

		HelpDebug.compare(15, this.countSquares(HelpDebug.str2array1("[[0,1,1,1],[1,1,1,1],[0,1,1,1]]")));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub
		HelpDebug.compare(7, this.countSquares(HelpDebug.str2array1("[[1,0,1],[1,1,0],[1,1,0]]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
//		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
